package com.zyf.chapter02.creation.singleton;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * <h3>日志</h3>
 * 该种方式需要手动加锁，以防止并发问题所导致的问题，日志文件被覆盖
 * @author yifeng
 */
public class Logger {
    private final FileWriter writer;

    public Logger() throws IOException {
        File file = new File("/user/yifeng/log.txt");
        // true 表示追加写入
        writer = new FileWriter(file, true);
    }

    public void log(String message) throws IOException {
        synchronized (Logger.class) {
            writer.write(message);
        }
    }

}
